<?php
class Categories extends MY_Controller{
	
	function __construct()
	{
		parent::__construct();
	}
	
	
	function index()
	{
		$data['title'] = $this->title('Categories');
		$data['category'] = category::get_topLevel();
		$this->load->view($this->theme.'/header',$data);
		$this->load->view($this->theme.'/category/index');
		$this->load->view($this->theme.'/footer');
	}
	
	function view ( $cat_id = null )
	{
		if($cat_id == null){
			redirect('categories');
		}else{
			$cat = category::find_by_id($cat_id);
			$data['title'] = $this->title($cat->name_en.' - Categories');
			$data['category'] = category::get_topLevel();
			$data['product'] = product::find('all', array('conditions'=>array('category_id'=>$cat_id)));
			$this->load->view($this->theme.'/header',$data);
			$this->load->view($this->theme.'/category/view');
			$this->load->view($this->theme.'/footer');
		}
	}
	
	function viewProduct( $cat_id = null,$cat_name = null, $product_id = null )
	{
		if($cat_id == null || $product_id == null){
			redirect('categories');
		}else{		
			$product = product::find_by_id($product_id);
			if($product){
			$data['product'] = $product;
			}else{
				show_404();
			}
			$data['title'] = $this->title($product->category->name_en.' - Categories');
			$data['category'] = category::get_topLevel();
			$data['photo'] = photo::find('all',array('conditions'=>array('product_id'=>$product_id),'order'=>'position asc'));
			$this->load->view($this->theme.'/header',$data);
			$this->load->view($this->theme.'/category/viewProduct');
			$this->load->view($this->theme.'/footer');
		}
	}
	
}